/**
 * @file main.c
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2023-12-27
 *
 * @copyright Copyright (c) 2023
 *
*/
#include "aiio_adapter_include.h"
#include "staWiFiConnect.h"
#include "led_dev.h"
#include "config_buttom.h"
#include "aiio_cloud_rtc.h"
#include "aiio_common.h"
#include "ble_config.h"

void aiio_main(void* params)
{
    staWiFiInit();  //WiFi 初始化
    devLedNetWorkdInit(); //网络灯初始化
    devLedCtrlInit();
    configButtomInit();  //配网按键初始化
    aiio_rtc_time_init(); //RTC初始化
    //从Flash 读取连接信息
    int config_len = aiio_flash_get_wifi_config_data(&wifi_config_data);
    //读取成功则发起连接，
    if (config_len>0) {
        //判断是wifi 名称和 token 是否存在，存在则连接
        if (wifi_config_data.ssid&&wifi_config_data.token) {
            cloud_data_s.device_token = wifi_config_data.token;
            cloud_data_s.mqtt_host = wifi_config_data.mqttip;
            cloud_data_s.mqtt_port = wifi_config_data.port;

            staWiFiConnect(wifi_config_data.ssid, wifi_config_data.password);
        }
        else {
            //不存在则配网
            bleConfigStart();
        }
    }
    else {
        //读取不到信息也进入配网
        bleConfigStart();
    }

    while (1)
    {
        aiio_os_tick_dealy(aiio_os_ms2tick(2000));
    }
}

